{{- $_ := set . "Version" ( splitList "/" .Values.global.doc_version | last ) }}
{{- $_ := set . "VersionURLNormalized" ( printf "%s" .Version | lower | replace "+" "-plus-" | replace "_" "-u-" ) }}
{{- $_ := set . "VersionDNSNormalized" ( printf "%s" .Version | lower | replace "+" "-plus-" | replace "_" "-u-" | replace "." "-dot-" ) }}
{{- $hostEn := .Values.global.url }}
{{- $hostRu := .Values.global.url_ru }}
{{- $hosts := list -}}
{{- $hosts = append $hosts (dict "Lang" "en" "URL" $hostEn) -}}
{{- $hosts = append $hosts (dict "Lang" "ru" "URL" $hostRu) -}}

{{- range $hosts }}
apiVersion: networking.k8s.io/v1
kind: Ingress
metadata:
  name: main-{{ .Lang }}
  annotations:
    nginx.ingress.kubernetes.io/from-to-www-redirect: "true"
    nginx.ingress.kubernetes.io/enable-cors: "true"
    nginx.ingress.kubernetes.io/cors-allow-origin: '*'
    nginx.ingress.kubernetes.io/configuration-snippet: |
      proxy_set_header X-Original-URI         $request_uri;
      ssi on;
      ssi_silent_errors on;
      rewrite ^/source/modules/(.*)$ /modules/$1 permanent;
      rewrite ^/documentation/v1/modules/490-virtualization/(examples|configuration|cr|faq).html(.*)$ /modules/virtualization/stable/$1.html$2 permanent;
      rewrite ^/documentation/v1/modules/490-virtualization/.*$ /modules/virtualization/stable/ permanent;
      rewrite ^/modules/([^./]+)/?$ /modules/$1/stable/ permanent;
      rewrite ^/modules/([^./]+)/((?!(alpha|beta|early-access|stable|rock-solid)).+)$ /modules/$1/stable/$2 permanent;
      rewrite ^(/en|/ru)?/documentation/v1\.3[0-7](\.[0-9]+)?(/.*)$ /documentation/v1.38$3 permanent;
      rewrite ^(/en|/ru)?(/documentation/v1\.[0-9]+)\.[0-9]+(/.*)$ $2$3 permanent;
      rewrite ^/ru/(.*) https://{{ $hostRu }}/$1 permanent;
      rewrite ^/en/(.*) https://{{ $hostEn }}/$1 permanent;
      {{- include "rewrites" . | nindent 6 }}
{{- if ne $.Values.web.env "web-production" }}
    nginx.ingress.kubernetes.io/auth-signin: https://$host/dex-authenticator/sign_in
    nginx.ingress.kubernetes.io/auth-url: https://{{ $.Chart.Name }}-{{ .Lang }}-dex-authenticator.deckhouse-{{ $.Values.web.env }}.svc.cluster.local/dex-authenticator/auth
    nginx.ingress.kubernetes.io/auth-response-headers: X-Auth-Request-User,X-Auth-Request-Email
{{- end }}
spec:
  ingressClassName: {{ pluck $.Values.web.dc_name $.Values.ingressClass | first | default $.Values.ingressClass._default | quote }}
  tls:
  - hosts:
      - {{ .URL }}
{{- if eq $.Values.web.env "web-production" }}
      - www.{{ .URL }}
{{- end }}
{{- if eq $.Values.web.env "web-production" }}
    secretName: star-{{ .URL | replace "." "-" }}
{{- else }}
    secretName: tls-{{ .URL }}
{{- end }}
  rules:
  - host: {{ .URL }}
    http:
      paths:
{{- if or (eq $.Values.web.dc_name "prod-sel") (eq $.Values.web.env "web-test" ) }}
      - path: /images
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /js
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /assets
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /gs
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /presentations
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /guides
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /modules
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /source
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /license_request_expired.html
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /license_request_success.html
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /documentation
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
{{- else }}
      - path: /
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
{{- end }}
      - path: /includes
        pathType: Prefix
        backend:
          service:
            name: backend
            port:
              name: http
      - path: /downloads/deckhouse-cli
        pathType: Prefix
        backend:
          service:
            name: frontend
            port:
              name: http
      - path: /documentation/v1
        pathType: Prefix
        backend:
          service:
            name: backend
            port:
              name: http
{{- if ne $.Values.web.env "web-production" }}
---
apiVersion: cert-manager.io/v1
kind: Certificate
metadata:
  name: {{ $.Chart.Name }}-{{ .Lang }}-cert
  annotations:
    werf.io/fail-mode: IgnoreAndContinueDeployProcess
    werf.io/track-termination-mode: NonBlocking
spec:
  certificateOwnerRef: false
  secretName: tls-{{ .URL }}
  issuerRef:
    kind: ClusterIssuer
    name: {{ pluck $.Values.web.dc_name $.Values.certificateClusterIssuer | first | default $.Values.certificateClusterIssuer._default }}
  commonName: {{ .URL }}
  dnsNames:
  - {{ .URL }}
---
apiVersion: deckhouse.io/v1alpha1
kind: DexAuthenticator
metadata:
  name: {{ $.Chart.Name }}-{{ .Lang }}
spec:
  applicationDomain: {{ .URL }}
  sendAuthorizationHeader: false
  applicationIngressCertificateSecretName: tls-{{ .URL }}
  applicationIngressClassName: {{ pluck $.Values.web.dc_name $.Values.ingressClass | first | default $.Values.ingressClass._default | quote }}
  keepUsersLoggedInFor: "720h"
{{- end }}
---
{{ end -}}
